package com.example.day07_zhongji.adapter;

import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.day07_zhongji.R;
import com.example.day07_zhongji.bean.WorkBean;

import java.util.List;

public class WorkAdapter extends RecyclerView.Adapter{
    private Context context;
    private List<WorkBean.ResultsBean> datas;
    private int a=0;
    private int b=1;
    private int c=2;

    public WorkAdapter(Context context, List<WorkBean.ResultsBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==a){
            View inflate = LayoutInflater.from(context).inflate(R.layout.work_tiem, parent, false);
            return new ViewHolder(inflate);
        }else if (viewType==b){
            View inflate = LayoutInflater.from(context).inflate(R.layout.work_item1, parent, false);
            return new ViewHolder1(inflate);
        }else if (viewType==c){
            View inflate = LayoutInflater.from(context).inflate(R.layout.work_item2, parent, false);
            return new ViewHolder2(inflate);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        WorkBean.ResultsBean resultsBean = datas.get(position);
        int itemViewType = holder.getItemViewType();
        if (itemViewType==a){
            ViewHolder holder1= (ViewHolder) holder;
            holder1.text.setText(resultsBean.getTitle());
            Glide.with(context).load(resultsBean.getUrl_1()).into(holder1.image);
        }else if (itemViewType==b){
            ViewHolder1 holder1= (ViewHolder1) holder;
            holder1.text1.setText(resultsBean.getTitle());
            Glide.with(context).load(resultsBean.getUrl_1()).into(holder1.image1);
            Glide.with(context).load(resultsBean.getUrl_2()).into(holder1.image2);
            Glide.with(context).load(resultsBean.getUrl_3()).into(holder1.image3);
        }else if (itemViewType==c){
            ViewHolder2 holder1= (ViewHolder2) holder;
            holder1.text2.setText(resultsBean.getTitle());
            Glide.with(context).load(resultsBean.getUrl_1()).into(holder1.image2);
           // Glide.with(context).load(resultsBean.getUrl_1()).into(holder1.image3);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
            int type = datas.get(position).getType();
            Log.i("a", "getItemViewType: "+type);
            if (type==a){
                return a;
            }else if (type==b){
                return b;
            }else if (type==c){
                return c;
            }
        return 0;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;
        ImageView image;
        public ViewHolder(View inflate) {
            super(inflate);
            text=inflate.findViewById(R.id.item_text);
            image=inflate.findViewById(R.id.item_image);
        }
    }

    private class ViewHolder1 extends RecyclerView.ViewHolder {
        TextView text1;
        ImageView image1;
        ImageView image2;
        ImageView image3;
        public ViewHolder1(View inflate) {
            super(inflate);
            text1=inflate.findViewById(R.id.item1_text);
            image1=inflate.findViewById(R.id.iamge1);
            image2=inflate.findViewById(R.id.iamge2);
            image3=inflate.findViewById(R.id.iamge3);
        }
    }

    private class ViewHolder2 extends RecyclerView.ViewHolder {
        TextView text2;
        ImageView image2;
        ImageView image3;
        public ViewHolder2(View inflate) {
            super(inflate);
            text2=inflate.findViewById(R.id.item2_text);
            image2=inflate.findViewById(R.id.iamge11);
            image2=inflate.findViewById(R.id.iamge22);

        }
    }
}
